<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>定时任务管理</title>
<#include "../plugin.ftl">
<#--<#include "components/timed-task-upsert.ftl">-->
</head>
<body>
<div id="app" v-cloak style="margin: 30 auto 15">
    <div class="basic-container">
        <avue-crud :option="tableOption"
                   :data="tableData"
                   :table-loading="tableLoading"
                   :page="page"
                   ref="crud"
                   width="290"
                   @search-change="handleSearch"
                   @refresh-change="handleRefresh"
                   @size-change="handleSize"
                   @current-change="handleCurrent"
                   @search-reset="handleReset"
        >
            <template slot-scope="scope" slot="menuLeft">
                <el-button type="info" size="mini" @click.native="getQuartzSchedulerInfo()">获取调度器信息</el-button>
                <el-button type="primary" size="mini" @click.native="createJob()">创建Job</el-button>
            </template>
            <template slot-scope="scope" slot="menu">
                <el-button type="primary" size="mini" @click.native="resumeJob(scope)">恢复Job</el-button>
                <el-button type="primary" size="mini" @click.native="editJob(scope)">编辑Job</el-button>
                <el-button type="warning" size="mini" @click.native="stopJob(scope)">停止Job</el-button>
                <el-button type="warning" size="mini" @click.native="delJob(scope)">删除Job</el-button>
            </template>
        </avue-crud>
    </div>
    <el-dialog :title="JobDialogTitle" :visible.sync="dialogProps.showCreateJobDialog"
    >
        <avue-form :option="createJobFormOption"
                   v-model="jobInfo"
                   @submit="handleSaveJob">
        </avue-form>
    </el-dialog>
<#--<timed-task-upsert v-if="dialogProps.showCreateJobDialog"-->
<#--:id="activeId"-->
<#--@close="handleCloseCreateJobDialog"-->
<#--></timed-task-upsert>-->
</div>
<script type="text/javascript">
    const DICT = {
        STATUS: [
            {
                label: '待审核',
                value: 3
            },
            {
                label: '审核通过',
                value: 4
            },
            {
                label: '审核通过未上线',
                value: 7
            },
            {
                label: '下线',
                value: 0
            }
        ],
        RESOURCE_TYPE: [
            {
                label: '板块',
                value: 'pannel'
            },
            {
                label: '版面',
                value: 'tab'
            },
            {
                label: '轮播推荐位',
                value: 'block'
            },
            {
                label: '版面强插任务',
                value: 'tabForceInsert'
            }
        ],
        TIMED_TYPE: [
            {
                label: '定时上架',
                value: 'timely_shelving'
            },
            {
                label: '推荐池更新',
                value: 'recommend_pool_update'
            },
            {
                label: '版面强插',
                value: 'tab_force_insert'
            }
        ],
    };
    var timedTaskOption = {
        width: '100%',
        height: 'auto',
        labelWidth: '120',
        menuWidth: '440',
        index: false,
        border: true,
        selection: false,
        expand: false,
        stripe: false,
        page: true,
        menuAlign: 'center',
        align: 'left',
        menu: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        column: [
            {
                label: '自增ID',
                prop: 'id',
                width: '100',
                span: 24,
                search: true,
                visdiplay: false
            },
            {
                label: '资源ID',
                prop: 'resourceId',
                span: 24,
                search: true
            },
            {
                label: '资源类型',
                prop: 'resourceType',
                span: 24,
                type: 'select',
                search: true,
                dicData: DICT.RESOURCE_TYPE
            },
            {
                label: '资源版本',
                prop: 'resourceVersion',
                span: 24
            },
            {
                label: '状态',
                prop: 'status',
                span: 24,
                type: 'select',
                search: true,
                dicData: DICT.STATUS
            },
            {
                label: '创建时间',
                prop: 'createTime',
                span: 24,
                width: '180',
                type: "datetime",
                search: true,
                visdiplay: false,
                valueFormat: 'yyyy-MM-dd HH:mm:ss',
                format: 'yyyy-MM-dd HH:mm:ss'
            },
            {
                label: '投放时间',
                prop: 'releaseTime',
                span: 24,
                width: '180',
                type: "datetime",
                search: true,
                valueFormat: 'yyyy-MM-dd HH:mm:ss',
                format: 'yyyy-MM-dd HH:mm:ss'
            },
            {
                label: '定时类型',
                prop: 'timedType',
                span: 24,
                width: '180',
                type: 'select',
                search: true,
                dicData: DICT.TIMED_TYPE
            },
            {
                label: 'cron表达式',
                prop: 'cron',
                span: 24
            },
            {
                label: '更新时间',
                prop: 'lastUpdateDate',
                span: 24,
                width: '180',
                type: "datetime",
                search: true,
                visdiplay: false,
                valueFormat: 'yyyy-MM-dd HH:mm:ss',
                format: 'yyyy-MM-dd HH:mm:ss'
            },
        ]
    };
    var createJobFormOption = {
        labelWidth: 180,
        emptyBtn: false,
        column: [
            {
                label: '资源ID',
                prop: 'resourceId',
                width: '120',
                span: 24,
                type: 'number',
                min: 101,
                rules: [{
                    required: true,
                    message: '请输入资源ID',
                    trigger: 'blur'
                }]
            },
            {
                label: '资源类型',
                prop: 'resourceType',
                width: '120',
                span: 24,
                type: 'select',
                dicData: DICT.RESOURCE_TYPE,
                rules: [{
                    required: true,
                    message: '请选择资源类型',
                    trigger: 'blur'
                }],
                disabled: true
            },
            {
                label: '资源版本',
                prop: 'resourceVersion',
                width: '120',
                span: 24,
                rules: [{
                    required: true,
                    message: '请输入资源版本',
                    trigger: 'blur'
                }]
            },
            {
                label: '状态',
                prop: 'status',
                width: '120',
                span: 24,
                type: 'select',
                dicData: DICT.STATUS,
                rules: [{
                    required: true,
                    message: '请选择资源状态',
                    trigger: 'blur'
                }],
                disabled: true
            },
            {
                label: '定时类型',
                prop: 'timedType',
                width: '120',
                span: 24,
                type: 'select',
                dicData: DICT.TIMED_TYPE,
                rules: [{
                    required: true,
                    message: '请选择定时类型',
                    trigger: 'blur'
                }],
                disabled: true
            },
            {
                label: 'cron表达式',
                prop: 'cron',
                width: '120',
                span: 24
            }
        ]
    };
    var app = new Vue({
                el: '#app',
                data: function () {
                    return {
                        activeId: undefined,
                        dialogProps: {
                            showCreateJobDialog: false
                        },
                        urls: {
                            //分页数据接口
                            pageList: '/v1/timedTask/pageList.html',
                            getDetailInfo: '/v1/timedTask/getDetailInfo.html',
                            saveJob: '/v1/timedTask/saveJob.html',
                            resumeJob: '/v1/timedTask/resumeJob.html',
                            stopJob: '/v1/timedTask/stopJob.html',
                            delJob: '/v1/timedTask/delJob.html',
                            getQuartzSchedulerInfo: '/v1/timedTask/getQuartzSchedulerInfo.html',
                        },
                        tableOption: {}, // 表格设置属性
                        tableData: [], // 表格的数据
                        tablePage: 1,
                        tableLoading: false,
                        tableObj: {},
                        page: {
                            total: 0, // 总页数
                            currentPage: 1, // 当前页数
                            pageSize: 10 // 每页显示多少条
                        },
                        searchForm: {}, //表单搜索条件,
                        currentPage: 1, //当前页
                        pageSize: 15,   //页数
                        jobInfo: {
                            id: undefined,
                            resourceId: undefined,
                            resourceType: 'pannel',
                            resourceVersion: undefined,
                            status: 4,
                            timedType: 'recommend_pool_update',
                            cron: '0 30 8 * * ?'
                        },
                        JobDialogTitle: ''
                    };
                },
                created: function () {
                    // 初始化数据格式
                    var _this = this;
                    _this.tableOption = timedTaskOption;
                    _this.handleList();
                },
                methods: {
                    handleJobInfo(data) {
                        if (this.activeId) {
                            this.JobDialogTitle = '编辑job'
                            createJobFormOption.column[0].disabled = true
                            this.jobInfo.id = data.id;
                            this.jobInfo.resourceId = data.resourceId;
                            this.jobInfo.resourceType = data.resourceType;
                            this.jobInfo.resourceVersion = data.resourceVersion;
                            this.jobInfo.status = data.status;
                            this.jobInfo.timedType = data.timedType;
                            this.jobInfo.cron = data.cron;
                        } else {
                            this.JobDialogTitle = '创建job'
                            createJobFormOption.column[0].disabled = false
                            this.jobInfo.id = undefined;
                            this.jobInfo.resourceId = undefined;
                            this.jobInfo.resourceType = 'pannel';
                            this.jobInfo.resourceVersion = undefined;
                            this.jobInfo.status = 4;
                            this.jobInfo.timedType = 'recommend_pool_update';
                            this.jobInfo.cron = '0 30 8 * * ?';
                        }
                    },
                    //创建Job
                    createJob() {
                        this.activeId = undefined;
                        this.handleJobInfo();
                        this.dialogProps.showCreateJobDialog = true;
                    },
                    handleSaveJob() {
                        if (this.jobInfo.timedType === 'recommend_pool_update' && this.jobInfo.cron === '') {
                            this.$message({
                                type: 'error',
                                message: 'cron表达式不能为空！'
                            });
                            return
                        }
                        const data = {jsonStr: JSON.stringify(this.jobInfo)};
                        this.ajax(this.urls.saveJob, data, function (result) {
                            if (result.success) {
                                this.$message({
                                    showClose: true,
                                    message: '保存成功',
                                    type: 'success'
                                })
                                this.dialogProps.showCreateJobDialog = false;
                                this.handleList();
                            } else {
                                this.$message(result.msg);
                            }
                        }.bind(this));
                    },
                    //关闭创建Job弹窗
                    handleCloseCreateJobDialog() {
                        this.dialogProps.showCreateJobDialog = false;
                    },
                    //查询任务信息
                    getQuartzSchedulerInfo: function () {
                        this.ajax(this.urls.getQuartzSchedulerInfo, {}, function (data) {
                            if (data.code === '0') {
                                alert(JSON.stringify(data.data));
                            } else {
                                this.$message(data.msg);
                            }
                        }.bind(this));
                    },
                    resumeJob: function (scope) {
                        var row = scope.row;
                        this.ajax(this.urls.resumeJob, {
                            id: row.id
                        }, function (data) {
                            if (data.code === '0') {
                                this.$message({
                                    type: 'success',
                                    message: '成功!'
                                });
                                this.handleList();
                            } else {
                                this.$message(data.msg);
                            }
                        }.bind(this));
                    },
                    //编辑job
                    editJob: function (scope) {
                        var row = scope.row;
                        this.ajax(this.urls.getDetailInfo, {
                            id: row.id
                        }, function (data) {
                            if (data.code === '0') {
                                this.activeId = row.id;
                                this.handleJobInfo(data.data)
                                this.dialogProps.showCreateJobDialog = true;
                            } else {
                                this.$message(data.msg);
                            }
                        }.bind(this));
                    }
                    ,
                    stopJob: function (scope) {
                        var row = scope.row;
                        var _this = this;
                        this.$confirm('是否确认停止Job，自增ID：' + row.id, '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(function () {
                            _this.ajax(_this.urls.stopJob, {
                                id: row.id
                            }, function (data) {
                                if (data.code === '0') {
                                    _this.$message({
                                        type: 'success',
                                        message: '停止成功!'
                                    });
                                } else {
                                    _this.$message(data.msg);
                                }
                            });
                        }).catch(function () {
                            _this.$message({
                                type: 'info',
                                message: '已取消操作'
                            });
                        });

                    },
                    delJob: function (scope) {
                        var row = scope.row;
                        var _this = this;
                        this.$confirm('是否确认删除Job，自增ID：' + row.id, '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(function () {
                            _this.ajax(_this.urls.delJob, {
                                id: row.id
                            }, function (data) {
                                if (data.code === '0') {
                                    _this.$message({
                                        type: 'success',
                                        message: '删除成功!'
                                    });
                                    _this.handleList();
                                } else {
                                    _this.$message(data.msg);
                                }
                            });
                        }).catch(function () {
                            _this.$message({
                                type: 'info',
                                message: '已取消操作'
                            });
                        });

                    },
                    /**
                     * 初始分页数据
                     */
                    handleList: function () {
                        var _this = this;
                        _this.tableLoading = true;
                        _this.ajax(_this.urls.pageList, {}, function (data) {
                            if (data.code === '0') {
                                _this.tableData = data.data.rows
                                _this.page.total = data.data.total
                                _this.tableLoading = false

                            } else {
                                _this.tableData = [];
                                _this.tableLoading = false;
                            }
                        });
                    }
                    ,
                    /**
                     * 刷新
                     */
                    handleRefresh: function () {
                        var _this = this;
                        _this.tableLoading = true;
                        _this.ajax(_this.urls.pageList, {
                            page: 1
                        }, function (data) {
                            if (data.code === '0') {
                                _this.tableData = data.data.rows
                                _this.page = {
                                    total: data.data.total,
                                    currentPage: 1, // 当前页数
                                    pageSize: 10 // 每页显示多少条
                                }
                                _this.tableLoading = false
                                _this.$message({
                                    showClose: true,
                                    message: '刷新成功',
                                    type: 'success'
                                })
                            } else {
                                _this.tableData = [];
                                _this.tableLoading = false;
                            }
                        });
                    }
                    ,
                    /**
                     * 搜索
                     * @param tableForm
                     */
                    handleSearch: function (searchForm, done) {
                        var _this = this;
                        _this.searchForm = searchForm;
                        _this.tableLoading = true;
                        var searchParams = _this.getSearchParams(searchForm);
                        searchParams.page = _this.page.currentPage;
                        searchParams.rows = _this.page.pageSize;
                        _this.ajax(_this.urls.pageList, searchParams
                                , function (data) {
                                    if (data.code === '0') {
                                        _this.tableData = data.data.rows
                                        _this.page.total = data.data.total
                                        _this.tableLoading = false
                                    } else {
                                        _this.tableData = [];
                                        _this.tableLoading = false;
                                    }
                                });
                        if(done){
                            done()
                        }
                    },
                    search() {
                        var searchParams = this.getSearchParams(this.searchForm);
                        searchParams.page = this.page.currentPage;
                        searchParams.rows = this.page.pageSize;
                        alert(JSON.stringify(searchParams))
                        this.ajax(this.urls.pageList, searchParams
                                , function (data) {
                                    if (data.code === '0') {
                                        this.tableData = data.data.rows
                                        this.page.total = data.data.total
                                        this.tableLoading = false
                                    } else {
                                        this.tableData = [];
                                        this.tableLoading = false;
                                    }
                                }.bind(this));
                    },
                    /**
                     * @title 数据更新
                     * @param row 为当前的数据
                     * @param index 为当前更新数据的行数
                     * @param done 为表单关闭函数
                     *
                     **/
                    handleUpdate: function (row, index, done) {
                        var _this = this;
                        $.ajax({
                            type: 'POST',
                            url: _this.urls.create,
                            data: JSON.stringify(row),
                            contentType: "application/json; charset=utf-8",
                            success: function (res) {
                                var data = JSON.parse(res);
                                if (data.code === '0') {
                                    _this.$message({
                                        showClose: true,
                                        message: '生成成功',
                                        type: 'success'
                                    })
                                } else {
                                    _this.$message(data.msg);
                                }
                            },
                            error: function (data) {
                                console.log(data);
                            }
                        });
                        done()
                    },
                    handleReset(){
                        this.page.currentPage = 1;
                        this.page.pageSize = 10;
                    },
                    handleSize: function (pageSize) {
                        this.page.pageSize = pageSize;
                        this.handleSearch(this.searchForm);
                    },
                    handleCurrent: function (currentPage) {
                        this.page.currentPage = currentPage;
                        this.handleSearch(this.searchForm);
                    },
                }
                ,
                mounted: function () {

                }
            })
    ;
</script>
</body>
</html>

